﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using ChuanYe.Core.Utils;
using ChuanYe.CountyFireManagement.Api.Models;
using ChuanYe.CountyFireManagement.BLL;
using ChuanYe.CountyFireManagement.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace ChuanYe.CountyFireManagement.Api.Controllers
{
    /// <summary>
    /// 火灾统计
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class FireStatisticsController : ControllerBase
    {
        /// <summary>
        /// 区县级别 单一统计
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic, dynamic> TVSingleStatistics(TVSingleStatisticsRequest obj)
        {
            ResultDto<dynamic, dynamic> result = new ResultDto<dynamic, dynamic>();
            try
            {
                if (obj.SelectedYears == null)
                {
                    obj.SelectedYears = new List<int>();
                }
                if (obj.SelectedYears.Count == 0)
                {
                    obj.SelectedYears.Add(DateTime.Now.Year);
                }
                obj.SelectedYears = obj.SelectedYears.Distinct().OrderByDescending(o => o).ToList();
                string tempStartTimeSplit = "/01/01 00:00:00";
                string tempEndTimeSplit = "/12/31 23:59:59";
                if (obj.StartTime == null)
                {
                    obj.StartTime = (DateTime.Now.Year + tempStartTimeSplit).ToDateTime();
                }
                else
                {
                    obj.StartTime = SundryHelper.StartTimeConvert(obj.StartTime.Value);
                }
                if (obj.EndTime == null)
                {
                    obj.EndTime = (DateTime.Now.Year + tempEndTimeSplit).ToDateTime();
                }
                else
                {
                    obj.EndTime = SundryHelper.EndTimeConvert(obj.EndTime.Value);
                }
                string exclusiveYearStartTime = obj.StartTime.Value.ToString("/MM/dd HH:mm:ss");
                string exclusiveYearEndime = obj.EndTime.Value.ToString("/MM/dd HH:mm:ss");
                if (obj.StatisticsType == 1) //事发地点统计
                {
                    var linList = LinchpinBLL.Instance.GetSubLinchpin(Convert.ToInt32(obj.AddressCounty));
                    string[] xAxis = linList.Select(s => s.LinchpinName).ToArray();
                    List<dynamic> csList = new List<dynamic>();
                    for (int i = 0; i < obj.SelectedYears.Count; i++)
                    {
                        DateTime tempStartTime = (obj.SelectedYears[i] + exclusiveYearStartTime).ToDateTime();
                        DateTime tempEndTime = (obj.SelectedYears[i] + exclusiveYearEndime).ToDateTime();
                        var values = FireStatisticsBLL.Instance.TVAddressStatistics(obj.AddressCounty, tempStartTime, tempEndTime);
                        var yAxis = new List<long>();
                        foreach (var item in linList)
                        {
                            var info = values.Where(p => p.AddressTown == item.LinchpinId.ToString()).FirstOrDefault();
                            yAxis.Add(info != null ? info.Count : 0);
                        }
                        csList.Add(new { Year = obj.SelectedYears[i], Data = yAxis });
                    }
                    result.FirstParam = csList;
                    result.SecondParam = xAxis;
                }
                else if (obj.StatisticsType == 2)//事发时间统计
                {
                    List<dynamic> tsList = new List<dynamic>();
                    for (int i = 0; i < obj.SelectedYears.Count; i++)
                    {
                        DateTime tempStartTime = (obj.SelectedYears[i] + exclusiveYearStartTime).ToDateTime();
                        DateTime tempEndTime = (obj.SelectedYears[i] + exclusiveYearEndime).ToDateTime();
                        var values = FireStatisticsBLL.Instance.TVTimeStatistics(obj.AddressCounty, tempStartTime, tempEndTime);

                        var yAxis = new List<long>();
                        int timeIndex = 0;
                        while (timeIndex < 23)
                        {
                            var info = values.Where(p => p.Hour == timeIndex).FirstOrDefault();
                            yAxis.Add(info != null ? info.Count : 0);
                            timeIndex++;
                        }
                        tsList.Add(new { Year = obj.SelectedYears[i], Data = yAxis });
                    }

                    int[] xAxisTime = new int[24];
                    for (int i = 0; i < xAxisTime.Length; i++)
                    {
                        xAxisTime[i] = i;
                    }
                    result.FirstParam = tsList;
                    result.SecondParam = xAxisTime;
                }
                else if (obj.StatisticsType == 3)//起火原因统计
                {
                    List<dynamic> fwList = new List<dynamic>();
                    var xAxisAlarm = FireCauseBLL.Instance.Get();
                    for (int i = 0; i < obj.SelectedYears.Count; i++)
                    {
                        var yAxis = new List<long>();
                        DateTime tempStartTime = (obj.SelectedYears[i] + exclusiveYearStartTime).ToDateTime();
                        DateTime tempEndTime = (obj.SelectedYears[i] + exclusiveYearEndime).ToDateTime();
                        var values = FireStatisticsBLL.Instance.TVCauseStatistics(obj.AddressCounty, tempStartTime, tempEndTime);
                        foreach (var item in xAxisAlarm)
                        {
                            var info = values.Where(p => p.CauseID == item.Key).FirstOrDefault();
                            yAxis.Add(info != null ? info.Count : 0);
                        }
                        fwList.Add(new { Year = obj.SelectedYears[i], Data = yAxis });
                    }
                    result.FirstParam = fwList;
                    result.SecondParam = xAxisAlarm.Select(s => s.Value).ToArray();
                }
                else if (obj.StatisticsType == 4)//接警途径
                {
                    List<dynamic> atList = new List<dynamic>();
                    var xAxisAlarm = FireAlarmTypeBLL.Instance.Get();
                    for (int i = 0; i < obj.SelectedYears.Count; i++)
                    {
                        var yAxis = new List<long>();
                        DateTime tempStartTime = (obj.SelectedYears[i] + exclusiveYearStartTime).ToDateTime();
                        DateTime tempEndTime = (obj.SelectedYears[i] + exclusiveYearEndime).ToDateTime();
                        var values = FireStatisticsBLL.Instance.TVAlarmTypeStatistics(obj.AddressCounty, tempStartTime, tempEndTime);
                        foreach (var item in xAxisAlarm)
                        {
                            var info = values.Where(p => p.AlarmTypeID == item.Key).FirstOrDefault();
                            yAxis.Add(info != null ? info.Count : 0);
                        }
                        atList.Add(new { Year = obj.SelectedYears[i], Data = yAxis });
                    }
                    result.FirstParam = atList;
                    result.SecondParam = xAxisAlarm.Select(s => s.Value).ToArray();
                }
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/FireStatistics/TVSingleStatistics Server Error";
                LogHelper.Error(result.Message, ex);
            }
            return result;
        }



        /// <summary>
        /// 区县级别 单一统计 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic, dynamic> TVSingleStatistics2020(TVSingleStatisticsRequest obj)
        {
            ResultDto<dynamic, dynamic> result = new ResultDto<dynamic, dynamic>();
            try
            {
                List<dynamic> seTimes = new List<dynamic>();
                if (obj.StartTime.HasValue && obj.EndTime.HasValue)
                {
                    seTimes.Add(new { StartTime=obj.StartTime.Value,EndTime=obj.EndTime.Value});
                }
                else 
                {
                    if (obj.SelectedYears == null)
                    {
                        obj.SelectedYears = new List<int>();
                    }
                    if (obj.SelectedYears.Count == 0)
                    {
                        obj.SelectedYears.Add(DateTime.Now.Year);
                    }
                    string tempStartTimeSplit = "/01/01 00:00:00";
                    string tempEndTimeSplit = "/12/31 23:59:59";
                    foreach (var item in obj.SelectedYears)
                    {
                        DateTime tempStartTime = (item + tempStartTimeSplit).ToDateTime();
                        DateTime tempEndTime = (item + tempEndTimeSplit).ToDateTime();
                        seTimes.Add(new { StartTime = tempStartTime, EndTime = tempEndTime });
                    }
                }
                if (obj.StatisticsType == 1) //事发地点统计
                {
                    var linList = LinchpinBLL.Instance.GetSubLinchpin(Convert.ToInt32(obj.AddressCounty));
                    string[] xAxis = linList.Select(s => s.LinchpinName).ToArray();
                    List<dynamic> csList = new List<dynamic>();
                    for (int i = 0; i < seTimes.Count; i++)
                    {
                        var values = FireStatisticsBLL.Instance.TVAddressStatistics(obj.AddressCounty,(DateTime)seTimes[i].StartTime, (DateTime)seTimes[i].EndTime);
                        var yAxis = new List<long>();
                        foreach (var item in linList)
                        {
                            var info = values?.Where(p => p.AddressTown == item.LinchpinId.ToString()).FirstOrDefault();
                            yAxis.Add(info != null ? info.Count : 0);
                        }
                        csList.Add(new { Year = ((DateTime)seTimes[i].StartTime).Year, Data = yAxis });
                    }
                    result.FirstParam = csList;
                    result.SecondParam = xAxis;
                }
                else if (obj.StatisticsType == 2)//事发时间统计
                {
                    List<dynamic> tsList = new List<dynamic>();
                    for (int i = 0; i < seTimes.Count; i++)
                    {
                        var values = FireStatisticsBLL.Instance.TVTimeStatistics(obj.AddressCounty, (DateTime)seTimes[i].StartTime, (DateTime)seTimes[i].EndTime);

                        var yAxis = new List<long>();
                        int timeIndex = 0;
                        while (timeIndex < 23)
                        {
                            var info = values.Where(p => p.Hour == timeIndex).FirstOrDefault();
                            yAxis.Add(info != null ? info.Count : 0);
                            timeIndex++;
                        }
                        tsList.Add(new { Year = ((DateTime)seTimes[i].StartTime).Year, Data = yAxis });
                    }

                    int[] xAxisTime = new int[24];
                    for (int i = 0; i < xAxisTime.Length; i++)
                    {
                        xAxisTime[i] = i;
                    }
                    result.FirstParam = tsList;
                    result.SecondParam = xAxisTime;
                }
                else if (obj.StatisticsType == 3)//起火原因统计
                {
                    List<dynamic> fwList = new List<dynamic>();
                    var xAxisAlarm = FireCauseBLL.Instance.Get();
                    for (int i = 0; i < seTimes.Count; i++)
                    {
                        var yAxis = new List<long>();
                        var values = FireStatisticsBLL.Instance.TVCauseStatistics(obj.AddressCounty, (DateTime)seTimes[i].StartTime, (DateTime)seTimes[i].EndTime);
                        foreach (var item in xAxisAlarm)
                        {
                            var info = values.Where(p => p.CauseID == item.Key).FirstOrDefault();
                            yAxis.Add(info != null ? info.Count : 0);
                        }
                        fwList.Add(new { Year = ((DateTime)seTimes[i].StartTime).Year, Data = yAxis });
                    }
                    result.FirstParam = fwList;
                    result.SecondParam = xAxisAlarm.Select(s => s.Value).ToArray();
                }
                else if (obj.StatisticsType == 4)//接警途径
                {
                    List<dynamic> atList = new List<dynamic>();
                    var xAxisAlarm = FireAlarmTypeBLL.Instance.Get();
                    for (int i = 0; i < seTimes.Count; i++)
                    {
                        var yAxis = new List<long>();
                        var values = FireStatisticsBLL.Instance.TVAlarmTypeStatistics(obj.AddressCounty, (DateTime)seTimes[i].StartTime, (DateTime)seTimes[i].EndTime);
                        foreach (var item in xAxisAlarm)
                        {
                            var info = values.Where(p => p.AlarmTypeID == item.Key).FirstOrDefault();
                            yAxis.Add(info != null ? info.Count : 0);
                        }
                        atList.Add(new { Year = ((DateTime)seTimes[i].StartTime).Year, Data = yAxis });
                    }
                    result.FirstParam = atList;
                    result.SecondParam = xAxisAlarm.Select(s => s.Value).ToArray();
                }
            }       
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/FireStatistics/TVSingleStatistics2020 Server Error";
                LogHelper.Error(result.Message, ex);
            }
            return result;
        }
        


        /// <summary>
        /// 区县级别 综合统计(待定)
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        ResultDto<dynamic, dynamic> TVSummaryStatistics(TVSingleStatisticsRequest obj)
        {
            ResultDto<dynamic, dynamic> result = new ResultDto<dynamic, dynamic>();
            try
            {
                       
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/FireStatistics/TVSummaryStatistics Server Error";
                LogHelper.Error(result.Message, ex);
            }
            return result;
        }
    }
}